package ui.panels;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFileChooser;

import org.apache.log4j.Logger;

import ui.flowManager.expirimentProperties.ExperimentProperties;
import ui.panels.PanelTypes.PanelType;
import ui.panels.commands.NewExperimentCommand;
import ui.panels.commands.OpenErrorCommand;
import ui.panels.commands.OpenMessageBoxCommand;
import ui.panels.commands.RunExperimentCommand;
import ui.panels.commands.SaveToXMLCommand;



/**
 * 
 * @author vladimir && alon
 *	
 *	The panel from experiment have the next options: 
 *			1. Run the Experiment
 *			2. Save the Experiment to XML file
 *			3. Start the Experiment again
 *			4. Exit the program
 */
public class RunExperimentPanel extends Panel{

	/**
	 * 
	 */

	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	private JButton run;
	private JButton save;
	private JButton newExp;
	private JButton exit;
	public static String newline = System.getProperty("line.separator");
	Logger log = Logger.getLogger(this.getClass());
	
	@Override
	public void show(ExperimentProperties props) {
		try {
			GroupLayout thisLayout = new GroupLayout((JComponent)this);
			this.setLayout(thisLayout);
			setPreferredSize(new Dimension(722, 480));
			{
				run = new JButton();
				run.setText("Run Experiment");
				run.setFont(new Font(Font.MONOSPACED, Font.BOLD, 18));
			}
			
				save = new JButton();
				save.setText("Save To XML");
				save.setFont(new Font(Font.MONOSPACED, Font.BOLD, 18));				
				
				newExp = new JButton();
				newExp.setText("New Experiment");
				newExp.setFont(new Font(Font.MONOSPACED,Font.BOLD,18));

				
				exit = new JButton();
				exit.setText("Exit");
				exit.setFont(new Font(Font.MONOSPACED,Font.BOLD,18));
			thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
				.addContainerGap(63, 63)
				.addComponent(run, GroupLayout.PREFERRED_SIZE, 63, GroupLayout.PREFERRED_SIZE)
				.addGap(32)
				.addComponent(save, GroupLayout.PREFERRED_SIZE, 63, GroupLayout.PREFERRED_SIZE)
				.addGap(37)
				.addComponent(newExp, GroupLayout.PREFERRED_SIZE, 63, GroupLayout.PREFERRED_SIZE)
				.addGap(41)
				.addComponent(exit, GroupLayout.PREFERRED_SIZE, 63, GroupLayout.PREFERRED_SIZE)
				.addContainerGap(55, Short.MAX_VALUE));
			thisLayout.setHorizontalGroup(thisLayout.createSequentialGroup()
				.addContainerGap(268, 268)
				.addGroup(thisLayout.createParallelGroup()
				    .addGroup(thisLayout.createSequentialGroup()
				        .addComponent(run, GroupLayout.PREFERRED_SIZE, 207, GroupLayout.PREFERRED_SIZE)
				        .addGap(0, 0, Short.MAX_VALUE))
				    .addComponent(save, GroupLayout.Alignment.LEADING, 0, 207, Short.MAX_VALUE)
				    .addGroup(thisLayout.createSequentialGroup()
				        .addComponent(newExp, GroupLayout.PREFERRED_SIZE, 207, GroupLayout.PREFERRED_SIZE)
				        .addGap(0, 0, Short.MAX_VALUE))
				    .addGroup(thisLayout.createSequentialGroup()
				        .addComponent(exit, GroupLayout.PREFERRED_SIZE, 207, GroupLayout.PREFERRED_SIZE)
				        .addGap(0, 0, Short.MAX_VALUE)))
				.addContainerGap(247, 247));

		} catch (Exception e) {
			log.debug(e.toString());
		}
		
		//call to setListenrs
		setListeners();
	}

	/**
	 * Set the listeners
	 */
	private void setListeners()
	{
		ActionListener actionListener = createActionListener();
		
		
		//set the save and run buttons
		save.addActionListener(actionListener);
		run.addActionListener(actionListener);
		exit.addActionListener(actionListener);
		newExp.addActionListener(actionListener);
	}
	
	private ActionListener createActionListener()
	{
		ActionListener actionListener = new ActionListener() {
			
			public void actionPerformed(ActionEvent event) {
				
				//case the experiment should be safe
				if(event.getSource() == save)
				{		
					JFileChooser fileChooser = new JFileChooser();
							
					//open dialog
					int choice = fileChooser.showSaveDialog(null);
						
					if(choice == JFileChooser.APPROVE_OPTION)
					{
						System.out.println("approved");
						File file = fileChooser.getSelectedFile();
							
						//save to XML
						SaveToXMLCommand saveToXMLCommand = new SaveToXMLCommand();
						saveToXMLCommand.setFile(file);
						saveToXMLCommand.execute();
						//open to user panel with message box 
						OpenMessageBoxCommand messageBoxCommand = new OpenMessageBoxCommand();
						messageBoxCommand.setMeesage("The Experiment saved");
						messageBoxCommand.execute();
								
					}					
				}
				
				//case to run the experiment
				else if (event.getSource() == run)
				{
					//create && execute the command that run the experiment 
					RunExperimentCommand runExperimentCommand = new RunExperimentCommand();
					try {
						//run the experiment
						runExperimentCommand.execute();

						OpenMessageBoxCommand messageBoxCommand = new OpenMessageBoxCommand();
						messageBoxCommand.setMeesage("The Experiment ended");
						messageBoxCommand.execute();
						
					} catch (Exception e) {
						//log the exception and print fail to user
						log.debug(e.toString());
						OpenErrorCommand errorCommand = new OpenErrorCommand();
						errorCommand.setMeesage("The Experiment was failed");
						errorCommand.execute();	
					}
					
					
				}
				//case to start new experiment
				else if(event.getSource() == newExp)
				{
					NewExperimentCommand newExperiment = new NewExperimentCommand();
					newExperiment.execute();
				}
				//case exit the program
				else if (event.getSource() == exit)
				{
					System.exit(0);
				}
			}
		};
		
		return actionListener;
	}
	
	@Override
	public PanelType getPanelType() {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public void setFromPropertiesConfig(HashMap<String, String> map) {
		// TODO Auto-generated method stub		
	}
	
}
